package com.kdtech.analyse.Bbs;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 * http://club.sohu.com/ 搜狐社区
 *
 * @author allen
 */
public class SohuBbsAnalyse implements AnalyseNews {

	Map<String,String> map = new HashMap<String,String>();

	public static HashMap<String,String>  getLinks(String url,String html){
			try {
				if(StringUtils.isNotBlank(html)){
					HashMap<String,String> links = new HashMap<String, String>();
					String[] substringsBetween = StringUtils.substringsBetween(html, "<script>$.club.render.fill(", ");</script>");
					for (String string : substringsBetween) {
						if(string.startsWith("{\"tag\":\"#bbs_list\"")
								||string.startsWith("{\"tag\":\"#bbs_hosthreads\""))
						{
							JSONObject json = JSONObject.parseObject(string);
							String tmp = json.getString("content");
							Document parse = Jsoup.parse(tmp);
//						System.out.println(parse);
							Elements select = parse.select("a");
							for (Element element : select) {
								String link = element.attr("href");
								link = CrawlHTML.complementURL(link, url, parse, false);
								if(StringUtils.isNotBlank(link)){
									String text = element.text();
									if(StringUtils.isBlank(text)){
										text = element.attr("title");
									}
									links.put(link, text);
								}
							}
						}
					}
					return links;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return null;
	}

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://club.*.sohu.com/[?]action=read.*&forumid=.*&threadid=.*",
				"http://club.*sohu.com/.*thread/.*",
				//http://club.news.sohu.com/win_gifts/thread/39da2e8sr10
				"http://.*.auto.sohu.com/.*/thread.*-1.shtml",
				"http://q.sohu.com/forum/[0-9]*/topic/[0-9]*",
				"http://.*.club.sohu.com/.*/thread/.*",
				"http://s.club.sohu.com/\\?action=link.*&forumid=.*&threadid=.*",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}


	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Integer clickNum = 0;
		Integer commentNum = 0;

		String commentNumstr = null;
		String clickComentstr = null;
		String author=null;

		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("title").text();
		if (title == null || title.trim().length() == 0) {
		}
		if(title.endsWith("- 搜狐社区")){
			title = StringUtils.substringBeforeLast(title, "- 搜狐社区");
		}else{
			if (title.indexOf("-") != -1) {
				title = title.substring(0, title.indexOf("-"));
			}
		}

		content = HtmlCleaner.getContentHtml(url,  doc.select("div#postList talbe#topic.content"));
		if (content == null || content.trim().length() == 0) {
			content = HtmlCleaner.getContentHtml(url,  doc.select("table#post_0.viewpost tbody tr td div.wrap"));
			if (content == null || content.trim().length() == 0) {
				content = HtmlCleaner.getContentHtml(url, doc.select("table#post_0.viewpost tbody tr td div.wrap"));
				if (StringUtils.isBlank(content)) {
					content = HtmlCleaner.getContentHtml(url,  doc.select("div#contentbody div.leftFrame div.leftContainer div.leftContent div#bbs_postlist"));
					if (StringUtils.isBlank(content)) {
						if(htmltxt.indexOf("<script>$.club.render.fill")!=-1){
							content=htmltxt.substring(htmltxt.indexOf("<script>$.club.render.fill("));
							content=content.replace("</html>", "");
							content=content.replace("<script>$.club.render.fill(", "");
							content=content.replace(");</script>", "");
							JSONObject jasonObj=null;
							try{
								jasonObj=JSONObject.parseObject(content);
							}catch (Exception e) {
								// TODO: handle exception
							}
							if(jasonObj!=null){
								content=jasonObj.get("content").toString();
								Document docContent = Jsoup.parse(content);

								Elements select = docContent.select("div#bbs_postlist table.viewpost");
								if(select!=null && select.size()!=0){
									content = HtmlCleaner.getContentHtml(url, select.get(0).select("div.wrap"));
									author=select.get(0).select("span#userinfo a").text();
								}
							}
						}
					}
				}
			}
		}



		String tempdate = doc.select("div#postList div#topic.content h2")
				.html();
		if (tempdate != null && tempdate.trim().length() != 0) {
			if (tempdate.indexOf("rel=\"") != -1) {
				tempdate = tempdate.substring(tempdate.indexOf("rel=\"")
						+ "rel=\"".length());
				if (tempdate.indexOf("\"") != -1) {
					tempdate = tempdate.substring(0, tempdate.indexOf("\""));
					try {
						date = Long.parseLong(tempdate) * 1000;
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		} else {
			tempdate = doc.select("div.t_wrap div.t_main div.hd span.lz_time")
					.html();
			date = DateUtils.matchDate(tempdate);
		}

		if (date == null) {
			tempdate=doc.select("html body div#wrapper div#innerWrapper div#mainWrapper div#innerMainWrapper div#contentWrapper-wide div#innerContentWrapper div#topic.box div.b_body div.box_content div.box_innerContent div.topicInfo div.topicOpt").html();
			date =  DateUtils.matchDate(tempdate);
		}
		if (date == null) {
			date = DateUtils.matchDate(htmltxt);
		}

		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);

		clickComentstr = doc.select("div.pageLite").text();
		if (clickComentstr != null && clickComentstr.trim().length() != 0) {
			if (clickComentstr.indexOf("阅读:") != -1) {
				String clickNumstr = clickComentstr.substring(clickComentstr
						.indexOf("阅读:")
						+ "阅读:".length());
				if (clickNumstr.indexOf(" ") != -1) {
					clickNumstr = clickNumstr.substring(0, clickNumstr
							.indexOf(" "));
					try {
						clickNum = Integer.parseInt(clickNumstr);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
			if (clickComentstr.indexOf("回复:") != -1) {
				commentNumstr = clickComentstr.substring(clickComentstr
						.indexOf("回复:")
						+ "回复:".length());
				if (commentNumstr.indexOf(" ") != -1) {
					commentNumstr = commentNumstr.substring(0, commentNumstr
							.indexOf(" "));
					try {
						commentNum = Integer.parseInt(commentNumstr);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		} else {
			clickComentstr = doc.select("div#t_head").text();
			if (clickComentstr != null && clickComentstr.trim().length() != 0) {
				if (clickComentstr.indexOf("/点") != -1) {
					String clickNumstr = clickComentstr
							.substring(clickComentstr.indexOf("/点")
									+ "/点".length());
					if (clickNumstr.indexOf("]") != -1) {
						clickNumstr = clickNumstr.substring(0, clickNumstr
								.indexOf("]"));
						try {
							clickNum = Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
				if (clickComentstr.indexOf("[回") != -1) {
					commentNumstr = clickComentstr
							.substring(clickComentstr.indexOf("[回")
									+ "[回".length());
					if (commentNumstr.indexOf("/") != -1) {
						commentNumstr = commentNumstr.substring(0,
								commentNumstr.indexOf("/"));
						try {
							commentNum = Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
			}else{
				clickComentstr=doc.select("h3.postitle span").html();

				if (clickComentstr != null && clickComentstr.trim().length() != 0) {
					if (clickComentstr.indexOf("阅读:") != -1) {
						String clickNumstr = clickComentstr.substring(clickComentstr.indexOf("阅读:")
								+ "阅读:".length());

						if (clickNumstr.indexOf(" ") != -1) {
							clickNumstr = clickNumstr.substring(0, clickNumstr
									.indexOf(" "));
							try {
								clickNum = Integer.parseInt(clickNumstr);
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
					}
					if (clickComentstr.indexOf("回复:") != -1) {
						commentNumstr = clickComentstr
								.substring(clickComentstr.indexOf("回复:")
										+ "回复:".length()).trim();
						try {
							commentNum = Integer.parseInt(commentNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}

				}

			}
		}



		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);


		bbs.setAuthor(author);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);

		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta  = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	
	
}
